define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function ($, undefined, Backend, Table, Form, Template) {
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: '/sys/bom/data',
                    add_url: '/sys/bom/add',
                    edit_url: '/sys/bom/edit',
                    del_url: '/sys/bom/del',
                    multi_url: 'data/multi.json',
                    "table": 'page',
                }
            });
            var table = $("#table");
            $('#config').on('click', function () {

                var length = table.bootstrapTable('getSelections').length;
                // if (length <= 0) {
                //     Layer.alert("<code>任何原料</code>");
                //     return;
                // }
                var selected = table.bootstrapTable('getSelections');
                var ids = [];
                selected.forEach(function (elt, index) {
                    ids.push(elt.id);
                })
                ids = ids.join(",");
                var production_id = production.id;
                var params = {
                    production_id: production_id,
                    bom_ids: ids
                };
                $.post('/sys/production/config', params, function (data) {
                    if (data.code == 1) {
                        parent.Toastr.info("操作成功");
                        var index = parent.Layer.getFrameIndex(window.name);
                        parent.Layer.close(index);
                        window.$(".btn-refresh").trigger("click");
                    } else {
                        parent.Toastr.error("添加失败");
                    }
                }, 'json');

            });
            let arr = [];
            if (production.bom_ids) {
                arr = production.bom_ids.split(",");
            }
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                sortName: 'id',
                columns: [
                    [
                        {
                            field: 'aaa', checkbox: true, formatter: function (value, row, index) {
                                if (arr) {
                                    if (arr.indexOf(row.id + "") != -1) {
                                        return {
                                            checked: true//设置选中
                                        };
                                    } else {
                                        return {
                                            checked: false//设置选中
                                        };
                                    }
                                }

                            }
                        },
                        {field: 'id', title: __('ID'), operate: false},
                        {field: 'name', title: __('名称'), operate: 'LIKE %...%', placeholder: '模糊搜索', style: 'width:200px'},
                        {field: 'code', title: __('编码'), operate: 'LIKE %...%', placeholder: '模糊搜索', style: 'width:200px'},
                        {field: 'model', title: __('型号'), operate: 'LIKE %...%', placeholder: '模糊搜索', style: 'width:200px'},
                        {field: 'specification', title: __('规格'), operate: 'LIKE %...%', placeholder: '模糊搜索', style: 'width:200px'},
                        {field: 'unit', title: __('单位'), operate: 'LIKE %...%', placeholder: '模糊搜索', style: 'width:200px'},
                        {field: 'create_time', title: __('创建时间'), operate: false, formatter: Table.api.formatter.datetime},
                        {field: 'update_time', title: __('修改时间'), operate: false, formatter: Table.api.formatter.datetime},
                        // {field: 'operate', title: __('操作'), events: Table.api.events.operate, formatter: Table.api.formatter.operate}

                    ]
                ],
                //禁用默认搜索
                search: false,
                //启用普通表单搜索
                commonSearch: true,
                //可以控制是否默认显示搜索单表,false则隐藏,默认为false
                searchFormVisible: true,
                exportDataType: 'selected',
                pagination: false
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});